﻿using System;
using System.Collections.Generic;
using System.Text;
using ASBCC.Database.Entity;

namespace ASBCC.Workflow.K2Manager
{
    public class K2Console
    {
        private SerializableDictionary _dataFields = new SerializableDictionary();
        public SerializableDictionary DataFields { get { return _dataFields; } }

        private K2AppForm _appForm = new K2AppForm();
        public K2AppForm AppForm { get { return _appForm; } }

        private K2WorkItem _workItem = new K2WorkItem();
        public K2WorkItem WorkItem { get { return _workItem; } }

        private K2ActionInfo _actionInfo = new K2ActionInfo();
        public K2ActionInfo ActionInfo { get { return _actionInfo; } }

        private string _comment;
        public string Comment { get { return _comment; } set { _comment = value; } }

        private K2AuditDestination _auditDestination;
        public K2AuditDestination AuditDestination { get { return _auditDestination; } set { _auditDestination = value; } }


        private List<K2User> _outputDestinationUsers = new List<K2User>();
        public List<K2User> OutputDestinationUsers { get { return _outputDestinationUsers; } set { _outputDestinationUsers = value; } }

        //添加的新的抄送人
        private string _ccUser;
        public string CcUser { get { return _ccUser; } set { _ccUser = value; } }

        private bool _IsExcuteAction = true;

        public bool IsExcuteAction
        {
            get
            {
                return _IsExcuteAction;
            }
        }

        public int Execute()
        {
            IActionHandler actionHandler = GetActionHandler();
            actionHandler.Execute();
            _IsExcuteAction=((ActionHandlerBase)actionHandler).IsExcuteAction;
            return actionHandler.ProcInstID;
        }

        private IActionHandler GetActionHandler()
        {
            if (ActionInfo.ActionType == K2ActionType.Submit)
            {
                return new SubmitActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.Resubmit)
            {
                return new ResubmitActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.PostSubmit)
            {
                return new PostSubmitActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.Approve)
            {
                return new ApproveActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.Reject)
            {
                return new RejectActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.Acknowledge)
            {
                return new AcknowledgeActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.Validate)
            {
                return new ValidateActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.Upload)
            {
                return new UploadActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }



            if (ActionInfo.ActionType == K2ActionType.Back)
            {
                return new ApproveActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.Review)
            {
                return new ReviewActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.Forward)
            {
                return new ForwardActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.ForwardBack)
            {
                return new ForwardBackActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers,CcUser);
            }

            if (ActionInfo.ActionType == K2ActionType.ReplyForwardBack)
            {
                return new ReplyForwardBackActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment);
            }

            if (ActionInfo.ActionType == K2ActionType.AskForMore)
            {
                return new AskForMoreActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers,CcUser);
            }

            if (ActionInfo.ActionType == K2ActionType.ReplyAskForMore)
            {
                return new ReplyAskForMoreActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment);
            }

            if (ActionInfo.ActionType == K2ActionType.Drawback)
            {
                return new DrawbackActionHandler(DataFields, AppForm, WorkItem, ActionInfo);
            }

            if (ActionInfo.ActionType == K2ActionType.Withdraw)
            {
                return new WithdrawActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.Discard)
            {
                return new DiscardActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.Redirect)
            {
                return new RedirectActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            if (ActionInfo.ActionType == K2ActionType.Goto)
            {
                return new GotoActionHandler(DataFields, AppForm, WorkItem, ActionInfo, Comment, AuditDestination, OutputDestinationUsers);
            }

            return null;
        }
    }

}
